package test.simple;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

//http://camel.apache.org/activemq.html
//http://code.google.com/p/activemq-in-action/source/browse/trunk/examples/src/main/resources/org/apache/activemq/book/ch7/pure-spring.xml?spec=svn38&r=38


public class LaunchJettyActiveMQ {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		org.apache.activemq.broker.BrokerService bs = new org.apache.activemq.broker.BrokerService();
		bs.setBrokerName("testBroker");
		bs.setPersistent(false);
		bs.setTransportConnectorURIs(new String[]{"tcp://localhost:61616"});
		bs.start();
		
		org.apache.activemq.ActiveMQConnectionFactory cf = new org.apache.activemq.ActiveMQConnectionFactory();
		cf.setBrokerURL("tcp://localhost:61616");
		org.apache.camel.component.jms.JmsConfiguration jmsComp = new org.apache.camel.component.jms.JmsConfiguration();
		jmsComp.setConnectionFactory(cf);
		jmsComp.setTransacted(false);
		jmsComp.setConcurrentConsumers(1);
		org.apache.activemq.camel.component.ActiveMQComponent comp = new org.apache.activemq.camel.component.ActiveMQComponent();
		comp.setConfiguration(jmsComp);
		
		CamelContext context = new DefaultCamelContext();
		context.addRoutes(new RouteBuilder() {
			public void configure() {
				//http://camel.apache.org/log.html
				//http://camel.apache.org/jetty.html
				from("jetty:http://localhost:8976/test").inOnly("activemq:queue:q1");
			}
		});
		context.addRoutes(new RouteBuilder() {
			public void configure() {
				//http://camel.apache.org/log.html
				//http://camel.apache.org/jetty.html
				from("activemq:queue:q1").to("log:org.apache.camel.example?level=INFO&showAll=true&multiline=true");
			}
		});		
		context.start();
	      URL u;

	         //------------------------------------------------------------//
	         // Step 2:  Create the URL.                                   //
	         //------------------------------------------------------------//
	         // Note: Put your real URL here, or better yet, read it as a  //
	         // command-line arg, or read it from a file.                  //
	         //------------------------------------------------------------//

	         u = new URL("http://localhost:8976/test?xml=3241526&type=tst");
	        // u = new URL("http://intra.tander");
	         //----------------------------------------------//
	         // Step 3:  Open an input stream from the url.  //
	         //----------------------------------------------//
	        InputStream is = u.openStream();
	        is.close();
	        
	         
	         
	
		Thread.sleep(50000);
		context.stop();
		bs.stop();

	}

}
